<template>
    <div class="loging">
        <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
            <el-form-item label="姓名" prop="name">
                <el-input v-model="ruleForm2.name"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="ruleForm2.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
                <el-button @click="resetForm('ruleForm2')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import { CHANGE_LOGING } from "../mutation-types";
import axios from "axios";
import URL from "../serviceAPI.config";
export default {
  name: "Loging",
  data() {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("账号不能为空"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm2.checkPass !== "") {
          //   this.$refs.ruleForm2.validateField("checkPass");
        }
        callback();
      }
    };
    return {
      ruleForm2: {
        pass: "",
        name: ""
      },
      rules2: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        name: [{ validator: checkName, trigger: "blur" }]
      }
    };
  },
  computed: {
    ...mapState(["userNames", "isLoging"])
  },
  methods: {
    ...mapMutations({
      changeLoging: "CHANGE_LOGING"
    }),
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
        
            this.changeLoging();
            localStorage.clear();
            localStorage.name = this.ruleForm2.name;
            localStorage.pass = this.ruleForm2.pass;

            axios.post(URL.userLogingApi, {
                userNames:localStorage.name,
                password: localStorage.pass
              })
              .then((response) =>{
                console.log(response.data);
                if(response.data.code == 200){
                  localStorage.token = response.data.token;
                  this.$message({
                    message: '登录成功!',
                    type: "success"
                  });
                   this.$router.push("/");
                }else{
                  this.$message.error("登录失败!");
                }
              })
              .catch((error) =>{
                console.log(error);
                this.$message.error("请输入正确的密码和账号");
              });
           
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped lang='scss'>
.loging {
  width: 25%;
  height: 100%;
  margin: 100px auto;
}
</style>